package zmee.bid.oa;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiParam;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
* @author lijiawang
* @Date 2022/12/8 16:47
* @Description:附件状态回传
*/
@ApiController(value = "pur", desc = "OA状态回传接口")
public class OABiddocStateApi {
    private static Log logger = LogFactory.getLog(OABiddocStateApi.class);
    @ApiPostMapping("/doStateService")
    public CustomApiResult<String> doBillsState(@ApiParam(value = "单据标识", required=true , example="src_biddoc") String sign,
                                                @ApiParam(value = "单据编号",required = true,example = "SRC2212030003")String billno,
                                                @ApiParam(value = "状态",required = true,example = "1=驳回，2=审核通过 ")Integer state,
                                                @ApiParam(value = "映像编码")String imageno){
        CustomApiResult<String> apiResult=new CustomApiResult<>();
        logger.info("====seriverstate==="+sign+billno+state);
        if(sign==null || billno==null || state == null){
            apiResult.setMessage("非法参数");
            apiResult.setErrorCode("非法参数");
            apiResult.setStatus(false);
            return apiResult;
        }
        QFilter qFilter=new QFilter("billno",QCP.equals,billno);
        DynamicObject single = BusinessDataServiceHelper.loadSingle(sign, "id,billon,", qFilter.toArray());
        if(single==null){
            apiResult.setErrorCode("未找到数据!");
            apiResult.setStatus(false);
            return apiResult;
        }
        DynamicObject  dynamicObject=BusinessDataServiceHelper.loadSingle(single.getPkValue(),single.getDynamicObjectType().getName());
        switch (sign){
            case "pmct_outcontract":
                if(imageno==null){
                    apiResult.setMessage("合同需传映像编码");
                    apiResult.setErrorCode("非法参数");
                    apiResult.setStatus(false);
                    return apiResult;
                }
                dynamicObject.set("zmee_imageno",imageno);
                DynamicObject contractstatus;
                if(state==2){
                    contractstatus = BusinessDataServiceHelper.loadSingle("pmct_contractstatus", "id", new QFilter("number", QCP.equals, "04").toArray());
                }else{
                    contractstatus = BusinessDataServiceHelper.loadSingle("pmct_contractstatus", "id", new QFilter("number", QCP.equals, "01").toArray());
                }
                 dynamicObject.set("contractstatus",contractstatus.getPkValue());
                DynamicObject[] loads = BusinessDataServiceHelper.load("pmct_contractlist", "id,status", new QFilter("contractid", QCP.equals, dynamicObject.getPkValue()).toArray());
                for (int i = 0; i < loads.length; i++) {
                    loads[i].set("status","C");
                }
                SaveServiceHelper.update(loads);
                SaveServiceHelper.update(dynamicObject);
                break;
            default:
                break;
        }
        if(state==2) {
            OperationResult operationResult = OperationServiceHelper.executeOperate("audit", dynamicObject.getDataEntityType().getName(), new DynamicObject[]{dynamicObject}, mutexIgnoreOption());
            logger.info("====operationResult==="+operationResult);
        }else if(state==1){
            OperationResult operationResult = OperationServiceHelper.executeOperate("unsubmit", dynamicObject.getDataEntityType().getName(), new DynamicObject[]{dynamicObject}, mutexIgnoreOption());
            logger.info("====operationResult==="+operationResult);
        }else {
            apiResult.setMessage("非法参数");
            apiResult.setErrorCode("非法参数");
            apiResult.setStatus(false);
            return apiResult;
        }
        apiResult.setMessage("200");
        apiResult.setStatus(true);
        return apiResult;
    }

    // 不执行警告级别校验器
    public OperateOption mutexIgnoreOption() {
        OperateOption operateOption = OperateOption.create();
        operateOption.setVariableValue(OperateOptionConst.IGNOREWARN, String.valueOf(true));
        operateOption.setVariableValue(OperateOptionConst.SKIP_BILLNO_VALIDATOR, String.valueOf(true));
        operateOption.setVariableValue(OperateOptionConst.MUTEX_IGNORE_VALIDATION, String.valueOf(true));
        return operateOption;
    }
}

